﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using FileObject;
using SetupFileObject;
using CalculateModelClasses;

namespace UanFileProceed
{
    //获得uan文件中的theta方向和phi方向上的增益和与增益对应的相位
    public class ReadUan
    {
        //提取UAN文件的内容，供直射计算使用

        public static string GetUan(TxObject tx, SetupObject setup)
        {
            StreamReader sr = new StreamReader(GetUanPath(tx,setup));
            string s = sr.ReadToEnd();
            sr.Close();
            return s;
        }
        public static string GetUan(RxObject rx, SetupObject setup)
        {
            StreamReader sr = new StreamReader(GetUanPath(rx, setup));
            string s = sr.ReadToEnd();
            sr.Close();
            return s;
        }

        //从UAN中获得四个list存放直射中电场计算需要的参数
        public static void GetGainPara(string uan, out List<string> thetaGain, out List<string> phiGain, out List<string> thetaPhase, out List<string> phiPhase)
        {
            thetaGain = new List<string>();
            phiGain = new List<string>();
            thetaPhase = new List<string>();
            phiPhase = new List<string>();
            int start = uan.IndexOf("end_<parameters>");
            int linelength = readline(uan, start);
            string[] st;
            string[] st1 = new string[50];
            string str;
            start += linelength + 2;
            int length = readline(uan, start);
            if (length > 44)
            {
                for (int i = 0; i <= 360; i++)
                    for (int j = 0; j <= 180; j++)
                    {
                        linelength = readline(uan, start);
                        str = uan.Substring(start, linelength);
                        st = str.Split(new Char[] { ' ' });
                        try
                        {
                            thetaGain.Add(st[4]);
                            phiGain.Add(st[6]);
                            thetaPhase.Add(st[8]);
                            phiPhase.Add(st[10]);
                            start += linelength + 2;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }

                    }
            }
            else
            {
                for (int i = 0; i <= 360; i++)
                    for (int j = 0; j <= 180; j++)
                    {
                        linelength = readline(uan, start);
                        str = uan.Substring(start, linelength);
                        st = str.Split(new Char[] { ' ' });
                        try
                        {
                            thetaGain.Add(st[4]);
                            phiGain.Add(st[6]);
                            thetaPhase.Add("0");
                            phiPhase.Add("0");
                            start += linelength + 2;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
            }
        }

        //判断求得场点所在的射线的theta角
        public static int GetThetaAngle(Point originPoint, Point targetPoint)
        {

            int theta;
            double A = Math.Sqrt(Math.Pow(targetPoint.x - originPoint.x, 2) + Math.Pow(targetPoint.y - originPoint.y, 2));
            double B = targetPoint.z - originPoint.z;
            double t = Math.Atan(A / B);
            if (originPoint.x == targetPoint.x && originPoint.y == targetPoint.y && originPoint.z == targetPoint.z)
                throw new Exception("场点位置与源点重合，无效！");
            else
            {
                if (t >= 0 && B >= 0)
                    theta = Convert.ToInt32(t * 180 / Math.PI);
                else
                    theta = Convert.ToInt32(t * 180 / Math.PI) + 180;
            }
            return theta;
        }

        //判断求得场点所在的射线的phi角
        public static int GetPhiAngle(Point originPoint, Point targetPoint)
        {
            int phi;
            double m = Math.Sqrt(Math.Pow(targetPoint.x - originPoint.x, 2) + Math.Pow(targetPoint.y - originPoint.y, 2));
            double p = Math.Asin((targetPoint.y - originPoint.y) / m);
            if (originPoint.x == targetPoint.x && originPoint.y == targetPoint.y && originPoint.z == targetPoint.z)
                throw new Exception("场点位置与源点重合，无效！");
            else
            {
                if (m == 0)
                    phi = 0;
                else if (p >= 0)
                    phi = Convert.ToInt32(p * 180 / Math.PI);
                else
                    phi = Convert.ToInt32(p * 180 / Math.PI) + 360;
            }
            return phi;
        }

        //根据发射机文件和setup文件得到UAN文件的路径

        static string GetUanPath(TxObject tx, SetupObject setup)
        {
            //用配置文件读取程序安装位置
            List<AntennaStp> antennas = setup.antenna.allAntennas;
            int i;
            for (i = 0; i < antennas.Count; i++)
            {
                if (tx.at.antennaNum == antennas[i].antenna)
                    break;
            }
            //DLL路径加上相对路径
            string path = GetDllPath()+"\\"+antennas[i].type+".uan";
            return path;
        }
        static string GetUanPath(RxObject rx, SetupObject setup)
        {
            //用配置文件读取程序安装位置
            List<AntennaStp> antennas = setup.antenna.allAntennas;
            int i;
            for (i = 0; i < antennas.Count; i++)
            {
                if (rx.at.antennaNum == antennas[i].antenna)
                    break;
            }
            //DLL路径加上相对路径
            string path = GetDllPath() + "\\" + antennas[i].type+".uan";
            return path;
        }

        //获取UAN文件DLL的路径

        static string GetDllPath()
        {
            string dllpath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
            dllpath = dllpath.Substring(8, dllpath.Length - 8);    // 8是 file:// 的长度
            return System.IO.Path.GetDirectoryName(dllpath);
        }

        //读取文件中的一行内容
        static int readline(string s, int start)
        {
            int length = 0;
            while (s[start + length] != '\r')
                length++;
            return length;
        }
    }
}
